const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';
const app=getApp();
//获得全局js变量
var util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        prev_url: '',
        userPhone:'',
        policy:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var prev_url = wx.getStorageSync('prevurl');
        console.log(prev_url);
        wx.removeStorageSync('prevurl');
        this.setData({
            prev_url:prev_url
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    userPolicy:function(){
        wx.navigateTo({
            url:'/pages/userpolicy/userpolicy'
        });
    },
    policy:function(){
        wx.navigateTo({
            url:'/pages/policy/policy'
        });
    },
    noPolicy:function(){
        wx.showToast({
            title: '请阅读并同意用户协议和隐私政策',
            icon:'none',
            duration: 1000
        })
    },
    checkboxChange:function(e){
        if(this.data.policy == 1){
            this.setData({
                policy:0
            });
        }else{
            this.setData({
                policy:1
            });
        }
    },
    getPhoneNumber (e) {
        var e = e.detail;
        var that=this;
        if(e.errMsg == 'getPhoneNumber:ok'){
            var code = e.code;
            var postdata = {code:code};
            util.httpRrquest(app.data.rooturl + 'weixin/getPhone', 'POST',postdata)
            .then(function (response) {
                console.log(response.data.data.data);
                var data = response.data.data.data;
                if(data.errcode == 0){
                    that.setData({
                        userPhone:data['phone_info']['phoneNumber']
                    });
                    that.saveUser();
                }else{
                    wx.showToast({
                        title: "获取失败",
                        icon: 'loading'
                    })
                }
                
            }, function (error) {
                console.log(error);
            });
        }
    },
    
    //增加微信用户
    saveUser: function () {
        var that=this
        var userFid = wx.getStorageSync('userFid') || 0;
        var postdata = {
            userFid: userFid,
            userPhone: that.data.userPhone
        }
        util.httpRrquest(app.data.rooturl + 'weixin/saveUser', 'POST',postdata)
        .then(function (response) {
            console.log(response.data.data);
            var data = response.data.data;
            app.globalData.userinfo = data;
            app.globalData.userId = data.id;
            app.globalData.userPhone = data.userPhone;
            wx.showToast({
                title: '登录成功',
                icon: 'success',
                duration: 500
            })
            console.log(that.data.prev_url);
            var prev = that.data.prev_url;
            if(prev == 'order'){
                setTimeout(() => {
                    wx.reLaunch({
                        url: '/pages/order/order' 
                    })
                }, 500)
            }else{
                setTimeout(() => {
                    wx.reLaunch({
                        url: '/pages/index/index' 
                    })
                }, 500)
            }
            
        }, function (error) {
            console.log(error);
        });
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var shareinfo = app.setShareMessage();
        console.log(shareinfo);
        return {
            title: shareinfo.title,
            path: shareinfo.path,
            imageUrl: shareinfo.imageUrl,
            success: (res) => {
                wx.showToast({
                    title: "分享成功",
                    icon: 'loading'
                })
            }
        }
    }
})